// websocket.js
export default function (Vue, options) {
  // 创建WebSocket实例
  const ws = new WebSocket('ws://localhost:16141');

  // 定义Vue全局方法和属性
  Vue.prototype.$sendMessage = (message) => {
    if (ws.readyState === WebSocket.OPEN) {
      ws.send(message);
    }
  };

  Vue.prototype.$subscribe = (event, callback) => {
    ws.addEventListener(event, callback);
  };

  // 初始化时订阅打开和关闭事件
  ws.addEventListener('open', function (event) {
    console.log('WebSocket is connected.');
  });

  ws.addEventListener('close', function (event) {
    console.log('WebSocket is closed.');
  });

  // 将WebSocket实例挂载到Vue全局
  Vue.prototype.$ws = ws;
}
